package com.masterclient.app.modules.goods.contract;

import android.text.TextUtils;
import android.widget.Toast;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.goods.ShopBean;
import com.eoner.baselibrary.utils.Utils;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/09
*/
public class ShopPresenter extends ShopContract.Presenter{

    @Override
    public void getSeller(String brand_id, String seller_id,String page,String page_size) {
        String type="";
        String id = "";
        if(!TextUtils.isEmpty(brand_id)){
            //type = "brand_id";
            type = "seller_id";
            id = brand_id;
        }else if(!TextUtils.isEmpty(seller_id)){
            type = "seller_id";
            id = seller_id;
        }
        Observable<ShopBean> observable = RetrofitUtil.getInstance().getSeller(NetEnvManager.BASE_SURL+"1/seller/index/"+type+"_"+id+"/"+page+"-"+page_size+".html");
        Disposable disposable = toSubscribe(observable, new Consumer<ShopBean>() {
            @Override
            public void accept(@NonNull ShopBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getSellerResult(bean.getData(),bean.getArgs().isPartner);
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void collectShop(String id) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().addFavorite(String.valueOf(Utils.getTimeStamp()),id,"6");
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showCollectSuccess();
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer());
        mRxManager.add(disposable);
    }

    @Override
    public void removeCollect(String id) {
        long timestamp = System.currentTimeMillis()/1000;
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().removeFavorite(String.valueOf(timestamp),id,"6");
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.removeCollectSuccess();
                }else {
                    Toast.makeText(Utils.getContext(),bean.getMsg(),Toast.LENGTH_SHORT).show();
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
            }
        });
        mRxManager.add(disposable);
    }


}